if (NOT WIN32)
  message(FATAL_ERROR "Windows only.")
endif ()

include(CheckSymbolExists)

if(NOT CMAKE_SIZEOF_VOID_P EQUAL 8)
  message(WARNING "Compiling Windows native binaries without 64-bit!")
endif()

# runtime library for linking: Mulit-threaded (/MT)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MT")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /MT")
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS_MINSIZEREL} /MT")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /MTd")

set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /OPT:REF /OPT:ICF")
set(CMAKE_EXE_LINKER_FLAGS_MINSIZEREL "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /OPT:REF /OPT:ICF")

check_symbol_exists("_M_AMD64" "" TARGET_X64)
if (TARGET_X64)
  set(BITS_SUFFIX "64")
  set(CMAKE_RC_FLAGS "${CMAKE_RC_FLAGS} /d X64")
else ()
  set(BITS_SUFFIX "")
endif ()

if(CMAKE_BUILD_TYPE STREQUAL "Debug")
  set(APP_TYPE "")
else()
set(APP_TYPE "WIN32")
endif()

set(OUTPUT_BASE_NAME jstart)

add_executable(${OUTPUT_BASE_NAME} 
    WIN32
    common.h
    common.cpp
    properties.h
    properties.cpp
    main.cpp
    manifest.xml
    jstart.rc
)

add_executable(${OUTPUT_BASE_NAME}c 
    common.h
    common.cpp
    properties.h
    properties.cpp
    main.cpp
    manifest.xml
    jstart.rc
)

# 设置DEBUG宏
target_compile_definitions(${OUTPUT_BASE_NAME} PUBLIC $<$<CONFIG:DEBUG>:DEBUG>)

# 设置CONSOLE宏
target_compile_definitions(${OUTPUT_BASE_NAME}c PUBLIC CONSOLE>)

# 设置输出目录
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

# 设置输出文件名
set_target_properties(${OUTPUT_BASE_NAME} PROPERTIES OUTPUT_NAME "${OUTPUT_BASE_NAME}${BITS_SUFFIX}")
set_target_properties(${OUTPUT_BASE_NAME}c PROPERTIES 
  OUTPUT_NAME "${OUTPUT_BASE_NAME}c${BITS_SUFFIX}"
  LINK_FLAGS "/SUBSYSTEM:CONSOLE" "/INCREMENTAL:NO" "/opt:REF"
)
